/*
 * Copyright (C) 2008-2009 Trinity <http://www.trinitycore.org/>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include "OutdoorPvPMgr.h"
#include "OutdoorPvPHP.h"
#include "OutdoorPvPNA.h"
#include "OutdoorPvPTF.h"
#include "OutdoorPvPZM.h"
#include "OutdoorPvPSI.h"
#include "OutdoorPvPEP.h"
#include "OutdoorPvPWG.h"
#include "ObjectMgr.h"
#include "Player.h"
#include "Policies/SingletonImp.h"

INSTANTIATE_SINGLETON_1( OutdoorPvPMgr );

OutdoorPvPMgr::OutdoorPvPMgr()
{
    m_UpdateTimer = 0;
    //sLog.outDebug("Instantiating OutdoorPvPMgr");
}

OutdoorPvPMgr::~OutdoorPvPMgr()
{
    //sLog.outDebug("Deleting OutdoorPvPMgr");
    for (OutdoorPvPSet::iterator itr = m_OutdoorPvPSet.begin(); itr != m_OutdoorPvPSet.end(); ++itr)
    {
        (*itr)->DeleteSpawns();
    }
}

void OutdoorPvPMgr::InitOutdoorPvP()
{
    // create new opvp
    OutdoorPvP * pOP = new OutdoorPvPHP;
    // respawn, init variables
    if(!pOP->SetupOutdoorPvP())
    {
        sLog.outDebug("OutdoorPvP : HP init failed.");
        delete pOP;
    }
    else
    {
        m_OutdoorPvPSet.push_back(pOP);
        sLog.outDebug("OutdoorPvP : HP successfully initiated.");
    }

    pOP = new OutdoorPvPNA;
    // respawn, init variables
    if(!pOP->SetupOutdoorPvP())
    {
        sLog.outDebug("OutdoorPvP : NA init failed.");
        delete pOP;
    }
    else
    {
        m_OutdoorPvPSet.push_back(pOP);
        sLog.outDebug("OutdoorPvP : NA successfully initiated.");
    }

    pOP = new OutdoorPvPTF;
    // respawn, init variables
    if(!pOP->SetupOutdoorPvP())
    {
        sLog.outDebug("OutdoorPvP : TF init failed.");
        delete pOP;
    }
    else
    {
        m_OutdoorPvPSet.push_back(pOP);
        sLog.outDebug("OutdoorPvP : TF successfully initiated.");
    }

    pOP = new OutdoorPvPZM;
    // respawn, init variables
    if(!pOP->SetupOutdoorPvP())
    {
        sLog.outDebug("OutdoorPvP : ZM init failed.");
        delete pOP;
    }
    else
    {
        m_OutdoorPvPSet.push_back(pOP);
        sLog.outDebug("OutdoorPvP : ZM successfully initiated.");
    }

    pOP = new OutdoorPvPSI;
    // respawn, init variables
    if(!pOP->SetupOutdoorPvP())
    {
        sLog.outDebug("OutdoorPvP : SI init failed.");
        delete pOP;
    }
    else
    {
        m_OutdoorPvPSet.push_back(pOP);
        sLog.outDebug("OutdoorPvP : SI successfully initiated.");
    }

    pOP = new OutdoorPvPEP;
    // respawn, init variables
    if(!pOP->SetupOutdoorPvP())
    {
        sLog.outDebug("OutdoorPvP : EP init failed.");
        delete pOP;
    }
    else
    {
        m_OutdoorPvPSet.push_back(pOP);
        sLog.outDebug("OutdoorPvP : EP successfully initiated.");
    }

    pOP = new OutdoorPvPWG;
    // respawn, init variables
    if(!pOP->SetupOutdoorPvP())
    {
        sLog.outDebug("OutdoorPvP : Wintergrasp init failed.");
        delete pOP;
    }
    else
    {
        m_OutdoorPvPSet.push_back(pOP);
        sLog.outDebug("OutdoorPvP : Wintergrasp successfully initiated.");
    }
}

void OutdoorPvPMgr::AddZone(uint32 zoneid, OutdoorPvP *handle)
{
    m_OutdoorPvPMap[zoneid] = handle;
}

void OutdoorPvPMgr::HandlePlayerEnterZone(Player *plr, uint32 zoneid)
{
    HandleEssenceOfWintergrasp(plr, zoneid);

    OutdoorPvPMap::iterator itr = m_OutdoorPvPMap.find(zoneid);
    if(itr == m_OutdoorPvPMap.end())
        return;

    if(itr->second->HasPlayer(plr))
        return;

    itr->second->HandlePlayerEnterZone(plr, zoneid);
    sLog.outDebug("Player %u entered outdoorpvp id %u", plr->GetGUIDLow(), itr->second->GetTypeId());
}

void OutdoorPvPMgr::HandlePlayerLeaveZone(Player *plr, uint32 zoneid)
{
    HandleEssenceOfWintergrasp(plr, zoneid, true);

    OutdoorPvPMap::iterator itr = m_OutdoorPvPMap.find(zoneid);
    if(itr == m_OutdoorPvPMap.end())
        return;

    // teleport: remove once in removefromworld, once in updatezone
    if(!itr->second->HasPlayer(plr))
        return;

    itr->second->HandlePlayerLeaveZone(plr, zoneid);
    sLog.outDebug("Player %u left outdoorpvp id %u",plr->GetGUIDLow(), itr->second->GetTypeId());
}

OutdoorPvP * OutdoorPvPMgr::GetOutdoorPvPToZoneId(uint32 zoneid)
{
    OutdoorPvPMap::iterator itr = m_OutdoorPvPMap.find(zoneid);
    if(itr == m_OutdoorPvPMap.end())
    {
        // no handle for this zone, return
        return NULL;
    }
    return itr->second;
}

void OutdoorPvPMgr::Update(uint32 diff)
{
    m_UpdateTimer += diff;
    if(m_UpdateTimer > OUTDOORPVP_OBJECTIVE_UPDATE_INTERVAL)
    {
        for (OutdoorPvPSet::iterator itr = m_OutdoorPvPSet.begin(); itr != m_OutdoorPvPSet.end(); ++itr)
            (*itr)->Update(m_UpdateTimer);
        m_UpdateTimer = 0;
    }
}

bool OutdoorPvPMgr::HandleCustomSpell(Player *plr, uint32 spellId, GameObject * go)
{
    for (OutdoorPvPSet::iterator itr = m_OutdoorPvPSet.begin(); itr != m_OutdoorPvPSet.end(); ++itr)
    {
        if((*itr)->HandleCustomSpell(plr,spellId,go))
            return true;
    }
    return false;
}

ZoneScript * OutdoorPvPMgr::GetZoneScript(uint32 zoneId)
{
    OutdoorPvPMap::iterator itr = m_OutdoorPvPMap.find(zoneId);
    if(itr != m_OutdoorPvPMap.end())
        return itr->second;
    else
        return NULL;
}

bool OutdoorPvPMgr::HandleOpenGo(Player *plr, uint64 guid)
{
    for (OutdoorPvPSet::iterator itr = m_OutdoorPvPSet.begin(); itr != m_OutdoorPvPSet.end(); ++itr)
    {
        if((*itr)->HandleOpenGo(plr,guid))
            return true;
    }
    return false;
}

void OutdoorPvPMgr::HandleGossipOption(Player *plr, uint64 guid, uint32 gossipid)
{
    for (OutdoorPvPSet::iterator itr = m_OutdoorPvPSet.begin(); itr != m_OutdoorPvPSet.end(); ++itr)
    {
        if((*itr)->HandleGossipOption(plr,guid,gossipid))
            return;
    }
}

bool OutdoorPvPMgr::CanTalkTo(Player * plr, Creature * c, GossipMenuItems gso)
{
    for (OutdoorPvPSet::iterator itr = m_OutdoorPvPSet.begin(); itr != m_OutdoorPvPSet.end(); ++itr)
    {
        if((*itr)->CanTalkTo(plr,c,gso))
            return true;
    }
    return false;
}

void OutdoorPvPMgr::HandleDropFlag(Player *plr, uint32 spellId)
{
    for (OutdoorPvPSet::iterator itr = m_OutdoorPvPSet.begin(); itr != m_OutdoorPvPSet.end(); ++itr)
    {
        if((*itr)->HandleDropFlag(plr,spellId))
            return;
    }
}

void OutdoorPvPMgr::HandleEssenceOfWintergrasp(Player* plr, uint32 ZoneId, bool LeaveZone)
{
    if (!plr) return;

    uint8 DefTeam = TEAM_NEUTRAL;
    bool IsWar = false;
    bool Northrend = false;

    OutdoorPvPMap::iterator itr = m_OutdoorPvPMap.find(NORTHREND_WINTERGRASP);
    if (itr != m_OutdoorPvPMap.end() && itr->second->GetTypeId() == OUTDOOR_PVP_WG)
    {
        DefTeam = ((OutdoorPvPWG*)itr->second)->getDefenderTeam();
        IsWar = ((OutdoorPvPWG*)itr->second)->isWarTime();
    }

    switch(ZoneId)
    {
        case NORTHREND_BOREAN_TUNDRA:
        case NORTHREND_CRYSTALSONG_FOREST:
        case NORTHREND_DALARAN:
        case NORTHREND_DRAGONBLIGHT:
        case NORTHREND_GRIZZLY_HILLS:
        case NORTHREND_HOWLING_FJORD:
        case NORTHREND_HROTHGARS_LANDING:
        case NORTHREND_ICECROWN:
        case NORTHREND_SHOLAZAR_BASIN:
        case NORTHREND_STORM_PEAKS:
        case NORTHREND_WINTERGRASP:
        case NORTHREND_ZULDRAK:
        case NORTHREND_AHNKAHET:
        case NORTHREND_AZJOL_NERUB:
        case NORTHREND_CULLING_OF_STRATHOLME:
        case NORTHREND_TRIAL_OF_THE_CHAMPION:
        case NORTHREND_DRAKTHARON_KEEP:
        case NORTHREND_GUNDRAK:
        case NORTHREND_NEXUS:
        case NORTHREND_OCULUS:
        case NORTHREND_VIOLET_HOLD:
        case NORTHREND_HALLS_OF_LIGHTNING:
        case NORTHREND_HALLS_OF_STONE:
        case NORTHREND_UTGARDE_KEEP:
        case NORTHREND_UTGARDE_PINNACLE:
        case NORTHREND_TRIAL_OF_THE_CRUSADER:
        case NORTHREND_NAXXRAMAS:
        case NORTHREND_OBSIDIAN_SANCTUM:
        case NORTHREND_ULDUAR:
        case NORTHREND_VAULT_OF_ARCHAVON:
            Northrend = true;
            break;
    }

    if (!Northrend)
    {
        if (plr->HasAura(SPELL_ESSENCE_OF_WG_1))
            plr->RemoveAurasDueToSpell(SPELL_ESSENCE_OF_WG_1);
        if (plr->HasAura(SPELL_ESSENCE_OF_WG_2))
            plr->RemoveAurasDueToSpell(SPELL_ESSENCE_OF_WG_2);

        return;
    }

    if (!LeaveZone && ZoneId == NORTHREND_WINTERGRASP)
    {
        if (!IsWar && plr->GetTeamId() == DefTeam)
        {
            if (plr->HasAura(SPELL_ESSENCE_OF_WG_1))
            {
                plr->RemoveAurasDueToSpell(SPELL_ESSENCE_OF_WG_1);
                plr->CastSpell(plr, SPELL_ESSENCE_OF_WG_2, true);
            }
            else if (!plr->HasAura(SPELL_ESSENCE_OF_WG_2))
                plr->CastSpell(plr, SPELL_ESSENCE_OF_WG_2, true);
        }
    }
    else if (LeaveZone && ZoneId == NORTHREND_WINTERGRASP && !IsWar && plr->GetTeamId() == DefTeam)
    {
        if (plr->HasAura(SPELL_ESSENCE_OF_WG_2))
            plr->RemoveAurasDueToSpell(SPELL_ESSENCE_OF_WG_2);
        if (!plr->HasAura(SPELL_ESSENCE_OF_WG_1))
            plr->CastSpell(plr, SPELL_ESSENCE_OF_WG_1, true);
    }
    else if (!IsWar && plr->GetTeamId() == DefTeam)
    {
        if (plr->HasAura(SPELL_ESSENCE_OF_WG_2))
            plr->RemoveAurasDueToSpell(SPELL_ESSENCE_OF_WG_2);
        if (!plr->HasAura(SPELL_ESSENCE_OF_WG_1))
            plr->CastSpell(plr, SPELL_ESSENCE_OF_WG_1, true);
    }
    else if ((!IsWar && plr->GetTeamId() != DefTeam) || IsWar)
    {
        if (plr->HasAura(SPELL_ESSENCE_OF_WG_1))
            plr->RemoveAurasDueToSpell(SPELL_ESSENCE_OF_WG_1);
        if (plr->HasAura(SPELL_ESSENCE_OF_WG_2))
            plr->RemoveAurasDueToSpell(SPELL_ESSENCE_OF_WG_2);
    }
}

void OutdoorPvPMgr::HandlePlayerResurrects(Player *plr, uint32 zoneid)
{
    OutdoorPvPMap::iterator itr = m_OutdoorPvPMap.find(zoneid);
    if(itr == m_OutdoorPvPMap.end())
        return;

    if(itr->second->HasPlayer(plr))
        itr->second->HandlePlayerResurrects(plr, zoneid);
}
